#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <event2/listener.h>
#include <event2/util.h>
#include <event2/event.h>

char string[1024] = "hello beibei2012\n";
static const uint16_t port = 1234;

static void signal_cb(evutil_socket_t sig, short events, void *user_data)
{
    struct event_base *base = user_data;
    struct timeval delay = {1, 0};

    printf("caught an interrupt signal, exit in 1sec.\n");

    event_base_loopexit(base, &delay);
}

static void conn_readcb(struct bufferevent *bev, void *user_data)
{
    int readn;

    readn = bufferevent_read(bev, string, sizeof(string));
    printf("read %d bytes from client\n", readn);
    printf("[%s]\n", string);

    bufferevent_enable(bev, EV_WRITE);
    bufferevent_disable(bev, EV_READ);

    bufferevent_write(bev, string, strlen(string));
}

static void conn_writecb(struct bufferevent *bev, void *user_data)
{
    struct evbuffer *output = bufferevent_get_output(bev);

    if (evbuffer_get_length(output) == 0) {
        //bufferevent_free(bev);
        bufferevent_enable(bev, EV_READ);
        bufferevent_disable(bev, EV_WRITE);
    }
}

static void conn_eventcb(struct bufferevent *bev, short events, void *user_data)
{
    if (events & BEV_EVENT_EOF) {
        printf("Connection closed. \n");
    } else if (events & BEV_EVENT_ERROR) {
        printf("got error on the connection. %s\n", strerror(errno));
    }

    bufferevent_free(bev);
}


static void listener_cb(struct evconnlistener *listener, evutil_socket_t fd, 
        struct sockaddr *sa, int socklen, void *user_data)
{
    struct event_base *base = user_data;
    struct bufferevent *bev;

    printf("new client: %s:%d\n", inet_ntoa(((struct sockaddr_in*)sa)->sin_addr),
            ntohs(((struct sockaddr_in*)sa)->sin_port));

    bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
    if (!bev) {
        fprintf(stderr, "new bufferevent error!\n");
        event_base_loopbreak(base);
        return;
    }

    bufferevent_setcb(bev, conn_readcb, conn_writecb, conn_eventcb, NULL);
    bufferevent_enable(bev, EV_READ);
    bufferevent_disable(bev, EV_WRITE);

    //bufferevent_write(bev, string, strlen(string));
}

int main(int argc, char *argv[])
{
    struct event_base *base;
    struct evconnlistener *listener;
    struct event *signal_event;
    struct sockaddr_in sin;

    base = event_base_new();
    if (!base) {
        fprintf(stderr, "init libevent error!\n");
        exit(1);
    }

    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_port = htons(port);

    listener = evconnlistener_new_bind(base, listener_cb, (void*)base, 
            LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE, -1,
            (struct sockaddr*)&sin, sizeof(sin));

    if (!listener) {
        fprintf(stderr, "could not create a listener!\n");
        exit(1);
    }

    signal_event = evsignal_new(base, SIGINT, signal_cb, (void*)base);

    if (!signal_event || event_add(signal_event, NULL) < 0) {
        fprintf(stderr, "create/add a signal event error!\n");
        exit(1);
    }

    printf("listen on port %d\n", port);

    event_base_dispatch(base);

    printf("OKOKOKOK\n");

    evconnlistener_free(listener);
    event_free(signal_event);
    event_base_free(base);

    printf("done\n");
    exit(0);
}


